package com.ciban.smarthome.module.db.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.ciban.smarthome.module.db.DbBaseDao;
import com.ciban.smarthome.module.entity.Scene;

import java.util.ArrayList;

/**
 * Created by wdl on 2015/11/6.
 */
public class SceneDao extends DbBaseDao {
    public SceneDao(Context context) {
        super(context, "scene");
    }

    /*serial_num integer(3),name varchar(30),icon varchar(50),eqiplist varchar(50)*/

    /**
     * 添加新的场景
     *
     * @param info
     * @return
     */
    public long addNewScene(Scene info) {
        ContentValues values = new ContentValues();
        values.put("serial_num", info.getSerial_no());
        values.put("name", info.getName());
        values.put("icon", info.getIcon_path());
        values.put("eqiplist", info.getEquip_list());
        values.put("host_no",info.getHost_serno());

        return insert(null, values);
    }


    /**
     * 获取所有的场景信息
     *
     * @return
     */
    public ArrayList<Scene> getSceneList() {
        ArrayList<Scene> list = new ArrayList<Scene>();
        Cursor cursor = query(new String[]{"_id", "serial_num", "name", "icon", "eqiplist","host_no"}, null, null, null);
        if (cursor != null) {
            Scene info;
            while (cursor.moveToNext()) {
                info = new Scene();
                info.setId(cursor.getString(0));
                info.setSerial_no(cursor.getInt(1));
                info.setName(cursor.getString(2));
                info.setIcon_path(cursor.getString(3));
                info.setEquip_list(cursor.getString(4));
                info.setHost_serno(cursor.getString(5));

                list.add(info);
            }
        }
        return list;
    }

    /**
     * 根据场景id 进行删除
     *
     * @param id
     * @return
     */
    public int delSceneById(String id) {
        return del("_id=?", new String[]{id});
    }
}
